package com.wjc.basemodule.base

import android.text.TextUtils
import android.view.View
import android.view.ViewStub
import android.widget.ImageView
import android.widget.TextView
import com.wjc.basemodule.R

/**
 * ClassName: com.wjc.basemodule.base.defaultPage
 * Description: loading  与缺省页
 * JcChen on 2022.07.27.15:55
 */
class DefaultPage(private val progressBar: View?, private val defaultViewStub: ViewStub? = null) {

    private var networkErrorOrNoDataView: View? = null


    fun startLoading() {
        progressBar?.visibility = View.VISIBLE
    }

    fun hideLoading() {
        progressBar?.visibility = View.GONE

    }

    fun hideDefaultPage() {
        networkErrorOrNoDataView?.visibility = View.GONE
    }


    fun showNoDataContent() {
        showNetworkErrorOrNoDataContent(showIcon = R.mipmap.ic_empty, showTips01 = "暂时没有数据")
    }

    fun showNetworkErrorContent(listener: View.OnClickListener?) {
        showNetworkErrorOrNoDataContent(showIcon = R.mipmap.ic_error, showTips01 = "数据请求失败", showTips02 = "请检查网络重试", listener = listener)
    }

    /**
     * @param showIcon         缺省图标
     * @param showTips01      提示
     * @param showTips02
     * @param listener       重试监听，不为空显示重试按钮
     */
    fun showNetworkErrorOrNoDataContent(showIcon: Int = 0, showTips01: String? = null, showTips02: String? = null, listener: View.OnClickListener? = null) {
        if (defaultViewStub == null) return
        if (networkErrorOrNoDataView == null) {
            networkErrorOrNoDataView = defaultViewStub.inflate()
        } else {
            networkErrorOrNoDataView?.visibility = View.VISIBLE
        }
        networkErrorOrNoDataView?.apply {
            val ivShowIcon = findViewById<ImageView>(R.id.noContentImage)
            val tvShowTips01 = findViewById<TextView>(R.id.tv_show_tips01)
            val tvShowTips02 = findViewById<TextView>(R.id.tv_show_tips02)
            val noContentButton = findViewById<TextView>(R.id.btn_refresh)
            if (showIcon > 0)
                ivShowIcon.setImageResource(showIcon)
            if (!TextUtils.isEmpty(showTips01)) {
                tvShowTips01.visibility = View.VISIBLE
                tvShowTips01.text = showTips01
            } else {
                tvShowTips01.visibility = View.GONE
            }
            if (!TextUtils.isEmpty(showTips02)) {
                tvShowTips02.visibility = View.VISIBLE
                tvShowTips02.text = showTips02
            } else {
                tvShowTips02.visibility = View.GONE
            }
            if (listener != null) {
                noContentButton.visibility = View.VISIBLE
                noContentButton.setOnClickListener(listener)
            } else {
                noContentButton.visibility = View.GONE
            }
        }
    }
}